package com.qianlou.qrcode.servlets;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/generate-qrcode-with-logo")
@MultipartConfig(
        fileSizeThreshold = 1024 * 1024 * 2,
        maxFileSize = 1024 * 1024 * 10,
        maxRequestSize = 1024 * 1024 * 100)
public class GenerateQRCodeWithLogo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String text = req.getParameter("text");

            Map map = new HashMap();
            map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            map.put(EncodeHintType.MARGIN, 1);

            MultiFormatWriter writer = new MultiFormatWriter();
            BitMatrix bitMatrix = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300, map);
            int width = bitMatrix.getWidth();
            int height = bitMatrix.getHeight();
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
                }
            }
            Part file = req.getPart("file");
            Image logo = ImageIO.read(file.getInputStream());
            int w = logo.getWidth(null);
            int h = logo.getHeight(null);
            w = Math.min(w, 60);
            h = Math.min(h, 60);
            // 缩放图片
            Image scaledInstance = logo.getScaledInstance(w, h, Image.SCALE_SMOOTH);
            Graphics2D graphics2D = image.createGraphics();
            int x = (width - w) / 2;
            int y = (height - h) / 2;
            graphics2D.drawImage(scaledInstance, x, y, null);
            Shape shape = new RoundRectangle2D.Float(x, y, w, h, 10, 10);
            graphics2D.setStroke(new BasicStroke(4f));
            graphics2D.setPaint(Color.WHITE);
            graphics2D.draw(shape);
            graphics2D.dispose();
            ImageIO.write(image, "png", resp.getOutputStream());
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
}
